// pages/home/home.js
import MyInfoApi from "../../api/myInfo";
import Dialog from "../../plugin/vant/dialog/dialog";
import StudyLoggerApi from "../../api/studyLogger";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewHeight:0,
    type:'1', //1.home 2.我的
    userInfo: {
      loginName: "",
      organName: "",
      realName: "",
      telNum: "",
      userId:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryUserInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goToSpecial(){
    wx.navigateTo({
      url: '/pages/specialStudy/specialStudy'
    })
  },
  goToCase(){
    wx.navigateTo({
      url: '/pages/caseStudy/caseStudy'
    })
  },
  changeType(e){
    let type = e.currentTarget.dataset['index'];
    this.setData({
      type:type
    })
  },
  goToPhone(){
    wx.navigateTo({
      url: `/pages/myPhone/myPhone?userId=${this.data.userInfo.userId}`
    })
  },
  goToRealName(){
    wx.navigateTo({
      url: `/pages/myRealName/myRealName?userId=${this.data.userInfo.userId}&realName=${this.data.userInfo.realName}`
    })
  },
  goToPassWord(){
    wx.navigateTo({
      url: `/pages/myPassWord/myPassWord?userId=${this.data.userInfo.userId}`
    })
  },
  goToScan(){
    wx.navigateTo({
      url: `/pages/organize/organize?type=userInfo&userId=${this.data.userInfo.userId}`
    })
  },
  /**
   * 查询用户信息
   */
  async queryUserInfo() {
    const user = JSON.parse(wx.getStorageSync('userInfo'))
    const param = {
      id: user.id
    }
    const res = await MyInfoApi.getUserInfo(param)
    const data = res.data
    const userInfo = this.data.userInfo
    userInfo.loginName = data.loginName
    userInfo.organName = data.organName
    userInfo.realName = data.realName
    userInfo.telNum = data.telNum
    userInfo.userId = data.id
    wx.setStorageSync('organizeCode',data.organCode)
    wx.setStorageSync('organizeName',data.organName)
    this.setData({
      userInfo
    })
  },
  /**
   * 退出登录
   */
  logout(){
    Dialog.confirm({
      title: '确认退出登录?',
    }).then(async () => {
      // on confirm
      wx.clearStorageSync()
      wx.reLaunch({
        url: '/pages/login/login'
      })
    }).catch(() => {
      // on cancel
    });
  }
})
